<template>
  <div>
    <el-dialog
      :title="obj.isAdd ? '活动添加' : '活动编辑'"
      :visible.sync="obj.isTrue"
      width="50%"
      @close="cancel()"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="活动名称">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="活动期限">
          <el-date-picker
            v-model="value1"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="一级分类">
          <el-select
            v-model="form.first_cateid"
            placeholder="请选择一级分类"
            @change="changeFir"
          >
            <el-option
              v-for="(item, index) in catelist"
              :key="index"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类">
          <el-select
            v-model="form.second_cateid"
            placeholder="请选择二级分类"
            @change="changeSec"
          >
            <el-option
              v-for="(item, index) in secCateArr"
              :key="index"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择商品">
          <el-select v-model="form.goodsid" placeholder="请选择商品">
            <el-option
              v-for="(item, index) in thirdCateArr"
              :key="index"
              :label="item.goodsname"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button type="primary" @click="submit()" v-if="obj.isAdd"
          >添 加
        </el-button>
        <el-button type="primary" @click="edit()" v-else>编 辑</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { successAlert, warningAlert } from "../../../util/alert";
import {
  cateList,
  goodsList,
  seckillAdd,
  seckillInfo,
  seckillEdit,
} from "../../../request/api";
export default {
  props: ["obj"],
  components: {},
  data() {
    return {
      value1: [],
      secCateArr: [],
      thirdCateArr: [],
      form: {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 1,
      },
    };
  },
  computed: {
    ...mapGetters({
      catelist: "cate/list",
      specsList: "specs/list",
      goodsList: "goods/list",
    }),
  },
  methods: {
    ...mapActions({
      reqGoodsList: "goods/reqChangeList",
      reqSeckillList: "seckill/reqChangeList",
      reqCateList: "cate/reqChangeList",
      reqSpecsList: "specs/reqChangeList",
    }),
    // 一级分类改变
    changeFir() {
      this.form.second_cateid = "";
      this.form.goodsid = "";
      this.thirdCateArr = [];
      cateList({ pid: this.form.first_cateid }).then((res) => {
        this.secCateArr = res.data.list;
      });
    },
    // 二级分类改变
    changeSec() {
      this.thirdCateArr = [];
      goodsList({
        fid: this.form.first_cateid,
        pid: this.form.second_cateid,
      }).then((res) => {
        this.thirdCateArr = res.data.list.filter(
          (item) => item.second_cateid == this.form.second_cateid
        );
        this.form.goodsid = "";
      });
    },
    // 清空封装
    empty() {
      this.form = {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 1,
      };
      this.value1 = [];
      this.secCateArr = [];
      this.thirdCateArr = [];
    },
    // 点击取消
    cancel() {
      (this.obj.isTrue = false), this.empty();
    },
    // 点击添加
    submit() {
      this.form.begintime = new Date(this.value1[0]).getTime();
      this.form.endtime = new Date(this.value1[1]).getTime();
      seckillAdd(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.cancel();
          this.reqSeckillList();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
    // 获取一条信息
    getOne(id) {
      seckillInfo({ id: id }).then((res) => {
        this.form = res.data.list;
        this.value1.push(Number(this.form.begintime));
        this.value1.push(Number(this.form.endtime));
        this.form.id = id;
        // 根据拿到的一级分类渲染二级分类
        cateList({ pid: this.form.first_cateid }).then((res) => {
          this.secCateArr = res.data.list;
        });
        // 根据拿到的二级分类渲染
        goodsList({
          fid: this.form.first_cateid,
          pid: this.form.second_cateid,
        }).then((res) => {
          this.thirdCateArr = res.data.list.filter(
            (item) => item.second_cateid == this.form.second_cateid
          );
        });
      });
    },
    // 编辑
    edit(id) {
      seckillEdit(this.form).then((res) => {
        console.log(this.form);
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.cancel();
          this.reqSeckillList();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
  },
  mounted() {
    this.reqCateList();
  },
};
</script>
<style scoped>
</style>